//
//  DViewController.m
//  MFTaobao
//
//  Created by Adiljan on 2016/10/21.
//  Copyright © 2016年 Adiljan. All rights reserved.
//

#import "DViewController.h"
#import "DetailViewCell.h"
#import "MyOrderTopTabBar.h"
#import "MJRefreshHeaderView.h"

#define TopTabBarH 50
#define screenH [UIScreen mainScreen].bounds.size.height
#define screenW [UIScreen mainScreen].bounds.size.width

@interface DViewController ()<MyOrderTopTabBarDelegate,UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property(nonatomic,weak)MyOrderTopTabBar* TopTabBar;
@property (weak, nonatomic)MJRefreshHeaderView* header;
@end

@implementation DViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, TopTabBarH, screenW, screenH-TopTabBarH) style:UITableViewStylePlain];
    
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
    _tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
//    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:@"DetailViewCell" bundle:nil] forCellReuseIdentifier:@"DetailViewCell"];
    
    NSString *comment = NSLocalizedString(@"comment", nil);
    NSString *image = NSLocalizedString(@"image", nil);
    NSString *detail = NSLocalizedString(@"detail", nil);
    NSArray* array  = @[image,comment,detail];
    MyOrderTopTabBar* tabBar = [[MyOrderTopTabBar alloc] initWithArray:array] ;
    tabBar.frame = CGRectMake(0,0, screenW, TopTabBarH);
    tabBar.backgroundColor = [UIColor whiteColor];
    tabBar.delegate = self;
    self.TopTabBar = tabBar;
    [self.view addSubview:tabBar];
    
    
    MJRefreshHeaderView* RheaderView = [MJRefreshHeaderView header];
    RheaderView.scrollView = _tableView;
    self.header = RheaderView;
    RheaderView.beginRefreshingBlock = ^(MJRefreshBaseView* refreshView){
        NSOperationQueue* Queue = [[NSOperationQueue alloc] init];
        [Queue addOperationWithBlock:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [UIView animateWithDuration:0.5 animations:^{
//                    self.MyScrollView.contentOffset = CGPointMake(0, 0);
                } completion:^(BOOL finished) {
//                    self.MyScrollView.scrollEnabled = YES;
                }];
                [self.header endRefreshing];
            });
        }];
    };
    
}
#pragma -- MyOrderTopTabBarDelegate(顶部标题栏delegate)
-(void)tabBar:(MyOrderTopTabBar *)tabBar didSelectIndex:(NSInteger)index{
    NSLog(@"点击了 －－－ %ld",index);
}

//完成拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
{
//    NSLog(@"scrollViewDidEndDragging");
    
    CGPoint offset = scrollView.contentOffset;
    CGRect bounds = scrollView.bounds;
    CGSize size = scrollView.contentSize;
    UIEdgeInsets inset = scrollView.contentInset;
    CGFloat currentOffset = offset.y + bounds.size.height - inset.bottom;
    
    CGFloat maximumOffset = size.height;
    //当currentOffset与maximumOffset的值相等时，说明scrollview已经滑到底部了。也可以根据这两个值的差来让他做点其他的什么事情
    //    NSLog(@"%lf",currentOffset-maximumOffset);
    CGFloat offsetY=currentOffset-maximumOffset;
    
    //    NSLog(@"%f",offset.y);
    if (offset.y<-60) {
        _headRefReshBlock();
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}
#pragma -- UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailViewCell"];
    return cell;
    
}
#pragma -- UITableViewDelegate
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
}


@end
